// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets;

import java.nio.ByteBuffer;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoPlayer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.network.GameClient;
import zombie.network.GameServer;

public class DeadPlayerPacket extends DeadCharacterPacket implements INetworkPacket {
    private byte playerFlags;
    private float infectionLevel;
    private IsoPlayer player;

    @Override
    public void set(IsoGameCharacter character) {
        super.set(character);
        this.player = (IsoPlayer)character;
        if (GameClient.bClient) {
            this.id = (short)this.player.getPlayerNum();
        }

        this.infectionLevel = this.player.getBodyDamage().getInfectionLevel();
        this.playerFlags = (byte)(this.playerFlags | (byte)(this.player.getBodyDamage().isInfected() ? 1 : 0));
    }

    @Override
    public void process() {
        if (this.player != null) {
            this.character.setHealth(0.0F);
            this.player.getBodyDamage().setOverallBodyHealth(0.0F);
            this.player.getBodyDamage().setInfected((this.playerFlags & 1) != 0);
            this.player.getBodyDamage().setInfectionLevel(this.infectionLevel);
            super.process();
        }
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        super.parse(b, connection);
        this.infectionLevel = b.getFloat();
        if (GameServer.bServer) {
            this.player = GameServer.getPlayerFromConnection(connection, this.id);
        } else if (GameClient.bClient) {
            this.player = (IsoPlayer)GameClient.IDToPlayerMap.get(this.id);
        }

        if (this.player != null) {
            this.character = this.player;
            this.parseCharacterInventory(b);
            this.character.setHealth(0.0F);
            this.character.getBodyDamage().setOverallBodyHealth(0.0F);
            this.character.getNetworkCharacterAI().setDeadBody(this);
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        super.write(b);
        b.putFloat(this.infectionLevel);
        this.writeCharacterInventory(b);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + String.format(" | isInfected=%b infectionLevel=%f", (this.playerFlags & 1) != 0, this.infectionLevel);
    }

    public IsoPlayer getPlayer() {
        return this.player;
    }
}
